We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

How can I avoid validation() that is used on $my_monkey->save() when executing $my_monkey->delete()

I am having a problem with deleting a species of monkey as I cannot get round validation used when creating.

class MyMonkeysCollection extends \Phalcon\Mvc\Collection
{
    use \Common\Models\Traits\MySoftDeleteable;

    /**
     *
     * @var string
     */
    public $family;

    /**
     *
     * @var string
     */
    public $genus;

    /**
     *
     * @var string
     */
    public $species;

    /**
     *
     * @var string
     */
    public $status;

    public function validation()
    {
        if ($this->speciesNotUnique()) {
                $message = new \Phalcon\Mvc\Model\Message(
                    "The species must be unique.",
                    "species",
                    "InvalidValue");
                $this->appendMessage($message);
                return false;
            }
    }

    protected function speciesNotUnique()
    {
        $monkey = self::findFirst([["species" => $this->species]]);

        if (isset($monkey->species)) {
            return true;
        }
        return false;
    }
}

When I $my_monkey->delete() then validation() is triggered.

How can skip validation() on delete() or what is the alternative?

edited Mar '16

Isn't your problem in SoftDelete trait or what does your SoftDelete exactly do?

Phalcon doesn't perform validation on model delete, only on save, you can check it here (_preSave method makes validation)

https://github.com/phalcon/cphalcon/blob/master/phalcon/mvc/model.zep

edited Mar '16

You can suppress validation and other events :( globally by setting

orm.events = 0


47.7k

@david-hubner:

My issue is that I would have it that the SoftDelete trait can be implemented in a later phase of development with no impact to the behaviour of the class it is used in besides introducing the status field and adding the behaviour.

I beleive that if SoftDelete prevents a true deletion of the record and updates the status field without triggering validation() or beforeSave() then it behaves more transparantly at the level of my_document->delete().

Maybe this is not safe.

In any case I readjusted my validation logic to handle the fact that validation is triggered in softDelete() and it now handles.



139

@baychae

I am having exectly same situation and egar to know your solution for readjusted validation logic.

Please expalain what is the solution to apply for avoiding validation for softDelete().

public function validation()
  {
    $validator = new Validation();

    $validator->add(
      'email', //your field name
      new EmailValidator([
        'model' => $this,
        'message' => 'Please enter a correct email address'
      ])
    );

    $validator->add(
      'email',
      new UniquenessValidator([
        'model' => $this,
        'message' => 'Sorry, That email is already taken',
      ])
    );

    return $this->validate($validator);
  }


47.7k

@mgr1286 . Hi sorry it was some time ago so cannot confirm. Apologies as I should have written what I did.

I beleive that in soft deleteable trait I did something like this:

<?php

namespace Common\Models\Traits;

use Phalcon\Mvc\Model\Validator\InclusionIn;

trait MySoftDeletable {

    public function beforeValidation() {

        $this->validate(new Inclusionin(
                array(
            "field" => "status",
            "domain" => array("A", "D", "L")
                )
        ));

        if ($this->validationHasFailed() == true) {
            return false;
        }
    }

    public function beforeDelete() {

        $this->validate(new Inclusionin(
                array(
            "field" => "status",
            "domain" => array("A", "D", "L")
                )
        ));

        if ($this->validationHasFailed() == true) {
            return false;
        }

    }

}

So I didn't call validation() in my trait.

@baychae

I am having exectly same situation and egar to know your solution for readjusted validation logic.

Please expalain what is the solution to apply for avoiding validation for softDelete().

public function validation()
 {
   $validator = new Validation();

   $validator->add(
     'email', //your field name
     new EmailValidator([
       'model' => $this,
       'message' => 'Please enter a correct email address'
     ])
   );

   $validator->add(
     'email',
     new UniquenessValidator([
       'model' => $this,
       'message' => 'Sorry, That email is already taken',
     ])
   );

   return $this->validate($validator);
 }


139

@baychae

Thank you for your prompt responce.

I will try the approach as per your reference and will let you know. For now going to add a custom soft delete function in the base model using PHQL.